package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.SysUserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Author  赵晴
 * Date 2020/07/08  15:27
 * Version 1.0
 */
@Service(interfaceClass = SysUserService.class)
@Transactional
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    SysUserDao sysUserDao;

    //分页查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage ( queryPageBean.getCurrentPage (), queryPageBean.getPageSize () );
        Page<SysUser> page = sysUserDao.findByCondition ( queryPageBean.getQueryString () );
        return new PageResult ( page.getTotal (), page );
    }

    //新增系统用户
    //同时需要设置用户和角色的关联关系
    @Override
    public void add(SysUser sysUser, Integer[] roleIds) {
        sysUserDao.add(sysUser);
        setRelation(sysUser.getId (),roleIds);
    }
    //设置用户和角色的关联关系
    private void setRelation(Integer sysUserId, Integer[] roleIds) {
        if(roleIds!=null && roleIds.length>0){
            for (Integer roleId : roleIds) {
                sysUserDao.setRelation(sysUserId,roleId);
            }
        }
    }
    //编辑用户回显
    @Override
    public SysUser findById(Integer id) {
        return sysUserDao.findById(id);
    }
    //根据用户id查询关联的角色id
    @Override
    public List<Integer> findRoleIdsBySyuUserId(Integer sysUserId) {
        return sysUserDao.findRoleIdsBySyuUserId(sysUserId);
    }
    //编辑系统用户
    @Override
    public void edit(SysUser sysUser, Integer[] roleIds) {
        //删除所有的关联关系
        sysUserDao.delRelation(sysUser.getId ());
        //维护新的关联关系
        setRelation ( sysUser.getId (),roleIds );
        //编辑检查组
        sysUserDao.edit(sysUser);
    }
    //删除系统用户
    @Override
    public void delById(Integer id) {
        //先解除用户和角色的关联关系
        sysUserDao.delRelation ( id );
        //在删除该系统用户
        sysUserDao.delById(id);
    }
    //新增校验手机号是否已经注册
    @Override
    public Result findByTelephone(String telephone) {
        SysUser sysUser=sysUserDao.findByTelephone(telephone);
        //未查询到
        if(sysUser==null){
            return new Result ( true,"" );
        }
        //查询到
        return new Result ( false, "该手机号已注册！！！" );
    }
    //编辑时校验手机号是否已经注册
    @Override
    public Result findByTelephoneAndId(String telephone, Integer id) {
        SysUser sysUser=sysUserDao.findByTelephoneAndId(telephone,id);
        //未查询到
        if(sysUser==null){
            return new Result ( true,"" );
        }
        //查询到
        return new Result ( false, "该手机号已注册！！!" );
    }
    //查询当前用户是否已被注册
    @Override
    public Result findBySysUserName(String username) {
        SysUser sysUser=sysUserDao.findBySysUserName(username);
        if(sysUser==null){
            return new Result ( true,"" );
        }
        return new Result ( false,"当前用户已被注册" );
    }
    //查询用户是否重名
    @Override
    public Result findBySysUserNameAndId(String username, Integer id) {
        SysUser sysUser=sysUserDao.findBySysUserNameAndId(username,id);
        if(sysUser==null){
            return new Result ( true,"" );
        }
        return new Result ( false,"当前用户已被注册" );
    }

}
